import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import pxtorem from 'postcss-pxtorem'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from '@vant/auto-import-resolver'
import { resolve } from 'path'
import vueJsx from '@vitejs/plugin-vue-jsx'

function pathResolve(dir) {
  return resolve(process.cwd(), '.', dir)
}

const loder_pxtorem = pxtorem({
  rootValue: 75, //换算基数，
  unitPrecision: 3, //允许REM单位增长到的十进制数字,小数点后保留的位数。
  propList: ['*'],
  exclude: /node_modules/i, //默认false，可以（reg）利用正则表达式排除某些文件夹的方法，例如/(node_module)/ 。如果想把前端UI框架内的px也转换成rem，请把此属性设为默认值
  selectorBlackList: ['.px'], //要忽略并保留为px的选择器，本项目我是用的vant ui框架，所以忽略他
  mediaQuery: false, //（布尔值）允许在媒体查询中转换px。
  minPixelValue: 1 //设置要替换的最小像素值
})
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      dts: false,
      eslintrc: { enabled: true },
      resolvers: [VantResolver()]
    }),
    Components({
      resolvers: [VantResolver()],
      dts: false
    })
  ],
  server: {
    port: 4567,
    host: true,
    open: true,
    proxy: {
      '/api': {
        // 接口代理
        target: 'http://192.168.3.30',
        changeOrigin: true
        // rewrite: (p) => p.replace(/^\/dev-api/, '')
      },
      '/group1': {
        // 图片oss代理
        target: 'http://192.168.3.30',
        changeOrigin: true
        // rewrite: (p) => p.replace(/^\/dev-api/, '')
      },
      '/live': {
        // 视频代理
        target: 'http://192.168.3.30:8866',
        changeOrigin: true
        // rewrite: (p) => p.replace(/^\/dev-api/, '')
      }
    }
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          hack: `true; @import (reference) "${pathResolve('src/style/var.less')}";`
        },
        javascriptEnabled: true
      }
    },
    postcss: {
      plugins: [loder_pxtorem]
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})
